﻿using UnityEngine;

namespace QFramework
{
    public static class TransformEx
    {
        /// <summary>
        /// 旋转至某个方向
        /// </summary>
        /// <param name="transform">需要旋转的组件</param>
        /// <param name="target">目标角度</param>
        /// <param name="rotationSpeed">旋转速度</param>
        public static void RotateToDir(this Transform transform, Quaternion target, float rotationSpeed)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, target, Time.deltaTime * rotationSpeed);
        }

        /// <summary>
        /// 插值旋转至某个方向
        /// </summary>
        /// <param name="transform">需要旋转的组件</param>
        /// <param name="target">目标角度</param>
        /// <param name="rotationSpeed">旋转速度</param>
        public static bool LerpRotateToDir(this Transform transform, Quaternion target, float rotationSpeed)
        {
            var step = Time.deltaTime * rotationSpeed;
            transform.rotation = Quaternion.Lerp(transform.rotation, target, step);
            if ((transform.rotation.eulerAngles - target.eulerAngles).sqrMagnitude < step)
            {
                transform.rotation = target;
                return true;
            }
            return false;
        }
    }
}